#include "Timer.h"
#include "KeyMatrix.h"

#include "stm32f0xx_rcc.h"
#include "stm32f0xx_misc.h"
#include "stm32f0xx_tim.h"

void Tim3BaseSet(void)
{
  TIM_TimeBaseInitTypeDef  Tim3Set;

  Tim3Set.TIM_Period = 50000;
  Tim3Set.TIM_Prescaler = 0;
  Tim3Set.TIM_ClockDivision = 0;
  Tim3Set.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3,&Tim3Set);
}

void TIM_NVIC_initial(void)
{
  NVIC_InitTypeDef nvic;

  nvic.NVIC_IRQChannel = 16;
  nvic.NVIC_IRQChannelPriority = 0;
  nvic.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&nvic);
}

void Timer_Initial(void)
{
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  TIM_NVIC_initial();
  
  Tim3BaseSet();

  TIM_ITConfig(TIM3,TIM_IT_CC3,ENABLE);
  TIM_Cmd(TIM3,ENABLE);    
}

#include "stm32f0xx.h"

static int timer = 0;
#include "Led.h"
void TIM3_IRQHandler(void)
{
    static int n = 0;
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
    n++;
    key_matrix_scan();
    if(n & 0x400)
    {
        timer++;
        //Led_Toggle();
    }
}

int get_timer(void)
{
    return timer;
}



// end of file -------------------

